/* Copyright 2012 Tobias Marschall
 *
 * This file is part of CLEVER.
 *
 * CLEVER is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CLEVER is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with CLEVER.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <boost/unordered_set.hpp>

#include "VariationIndex.h"

using namespace std;

VariationIndex::VariationIndex(const vector<Variation>& variations) {
	this->step_size = 5000;
	// PHASE 1: Add add all events to event lists
	for (size_t i=0; i<variations.size(); ++i) {
		const Variation& variation = variations[i];
		const string& chromosome = variation.getChromosome();
		if (chromosomes.find(chromosome) == chromosomes.end()) {
			chromosomes[chromosome] = chromosome_record_t();
		}
		chromosome_record_t& chromosome_record = chromosomes[chromosome];
		switch (variation.getType()) {
		case Variation::DELETION:
			chromosome_record.events.push_back(event_t(variation.getCoordinate1(), i, DELETION_START));
			chromosome_record.events.push_back(event_t(variation.getCoordinate2(), i, DELETION_END));
			break;
		case Variation::INSERTION:
			chromosome_record.events.push_back(event_t(variation.getCoordinate1(), i, INSERTION));
			break;
		default:
			assert(false);
		}
	}
	// PHASE 2: Sort event lists and create "checkpoints" of active events
	typedef boost::unordered_map<string,chromosome_record_t>::iterator map_it_t;
	for (map_it_t it = chromosomes.begin(); it != chromosomes.end(); ++it) {
		// cout << "================= " << it->first << " =================" << endl;
		vector<event_t>& events = it->second.events;
		vector<size_t>& event_list_indices = it->second.event_list_indices;
		sort(events.begin(), events.end(), event_compare_t());
		typedef vector<event_t>::const_iterator event_it_t;
		// boost::unordered_set<size_t> active_variations;
		size_t i = 0;
		size_t j = 0;
		for (event_it_t event_it=events.begin(); event_it!=events.end(); ++event_it) {
			for (;i*step_size<event_it->position; ++i) {
				event_list_indices.push_back(j);
			}
			j += 1;
		}
	}
}

VariationIndex::~VariationIndex() {
}

auto_ptr<vector<size_t> > VariationIndex::containedIn(const string& chromosome, size_t start, size_t end) {
	auto_ptr<vector<size_t> > result(0);
	if (chromosomes.find(chromosome) == chromosomes.end()) {
		return result;
	}
	const chromosome_record_t& chromosome_record = chromosomes[chromosome];
	size_t k = (start-1) / step_size;
	if (chromosome_record.event_list_indices.size() <= k) {
		return result;
	}
	const vector<event_t>& event_list = chromosome_record.events;
	boost::unordered_set<size_t> active_variations;
	for (size_t i=chromosome_record.event_list_indices[k]; i<event_list.size(); ++i) {
		const event_t& event = event_list[i];
		if (event.position < start) continue;
		if (event.position > end) break;
		switch (event.type) {
		case INSERTION:
			if (result.get() == 0) {
				result = auto_ptr<vector<size_t> >(new vector<size_t>());
			}
			result->push_back(event.variation_index);
			break;
		case DELETION_START:
			active_variations.insert(event.variation_index);
			break;
		case DELETION_END:
			if (active_variations.find(event.variation_index) != active_variations.end()) {
				if (result.get() == 0) {
					result = auto_ptr<vector<size_t> >(new vector<size_t>());
				}
				result->push_back(event.variation_index);
			}
			break;
		default:
			assert(false);
			break;
		}
	}
	return result;
}
